class_name UnitJellyfish
extends UnitFlyMagic


func shoot_magic():
	if not attack_target: return
	
	shoot_poison_bullet_magic()


func shoot_poison_bullet_magic():
	if not attack_target: return
	
	var target_unit = attack_target as UnitBase
	var target_position = ray_cast_point.global_position
	var target_dir = (attack_target.ray_cast_point.global_position - ray_cast_point.global_position).normalized()
	var magic = (ResourceManager.get_resource("particle_poison_bullet") as PackedScene).instantiate() as ParticlePoisonBullet
	
	var config = unit_data.config 
	magic.attack = config.main_attack
	magic.critical_rate = config.main_critical_attack_rate
	magic.critical_bonus = config.main_critical_attack_bonus
	magic.knockback = config.main_attack_knockback
	magic.poison_hit_rate = config.poison_hit_rate
	magic.poison_debuff_duration = config.poison_debuff_duration
	magic.poison_damage_per_second = config.poison_damage_per_second
	magic.sender = self
	var dir = target_dir
	magic.dir = dir
	magic.speed = 100
	magic.life_time = 3.0
	
	if config.main_attack_cam_shake:
		magic.hit_cam_shake_strength = config.main_attack_cam_shake_intensity
		magic.hit_cam_shake_duration = config.main_attack_cam_shake_duration
	
	level.particles_parent.add_child(magic)
	magic.global_position = target_position
	magic.set_is_enemy(self.is_enemy)
	#magic.set_collision_2_ground(false)


func play_magic_animation():
	animation_player.play("magic")
	

func pick_new_wander_target():
	if not self.is_enemy:
		target_pos = get_rand_follow_player_target_pos()
	else:
		var x_offset = randf_range(-wander_width/2, wander_width/2)
		var y_offset = randf_range(-wander_height/2, wander_height/2)
		target_pos = home_position + Vector2(x_offset, y_offset)
		
	timer = randf_range(1.0, change_dir_time)
	

func get_rand_follow_player_target_pos():
	var target_pos = Vector2.ZERO
	
	var player = level.player
	if player:
		target_pos = player.global_position + Vector2.UP * 24 + Vector2.RIGHT * -player.get_face() * 24
	else:
		target_pos = level.camera.global_position		

	target_pos += Vector2(randf_range(-12, 12), randf_range(-12, 12))
	return target_pos
	

func place_safely(unit: CharacterBody2D, step: Vector2):
	var max_checks := 20

	for i in range(max_checks):
		if not is_colliding_now(unit):
			break
		unit.global_position += step


func process_wander(delta: float):
	if self.is_enemy:
		super.process_wander(delta)
		return
		
	var distance = global_position.distance_to(level.camera.global_position)
	
	if distance > 200: 
		global_position = get_rand_follow_player_target_pos()
		place_safely(self, Vector2.DOWN)
		pick_new_wander_target()

	timer -= delta
	if timer <= 0.0 or global_position.distance_to(target_pos) < 3.0:
		pick_new_wander_target()
	
	navigation_agent_2d.target_position = target_pos
		
	if navigation_agent_2d.is_navigation_finished():
		return
	
	var next_pos = navigation_agent_2d.get_next_path_position()
	var dir = (next_pos - global_position).normalized()
	var target_velocity = dir * speed
	if navigation_agent_2d.avoidance_enabled:
		navigation_agent_2d.set_velocity(target_velocity)
	else:
		_on_navigation_agent_2d_velocity_computed(target_velocity, delta)
